package com.hd.crdao.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.hd.crdao.R;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;

/**
 *
 *@author Administrator
 *@date 2017/7/21 0021
 */
public class ReportDetailActivity extends BaseActivity{

    public ExpandHeadView mHead_report_detail;
    public WebView mWebview;
    public String mReportinfo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_report_detail;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initView() {
        mHead_report_detail = (ExpandHeadView) findViewById(R.id.head_report_detail);
        mWebview = (WebView) findViewById(R.id.webview);
        WebSettings settings = mWebview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAppCacheEnabled(false);
        mWebview.setWebChromeClient(new WebChromeClient());
    }

    @Override
    protected void initData() {
        mReportinfo = getIntent().getStringExtra("reportinfo");
        mWebview.loadUrl("file:///android_asset/html/report-detail.html");
        mWebview.addJavascriptInterface(new ProviderForJavaScript(), "reportBridge");
    }

    @Override
    protected void initListener() {
        mHead_report_detail.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private class ProviderForJavaScript {

        @JavascriptInterface
        public void sendMethod() {
            mWebview.post(new Runnable() {
                @Override
                public void run() {
                    mWebview.loadUrl("javascript:getDetail(" + mReportinfo + ")");
                }
            });
        }

        @JavascriptInterface
        public void goBackPressed() {
            finish();

        }
        @JavascriptInterface
        public void goDivideFeild(String param){
            Intent intent = new Intent(UIUtils.getContext(), BaoYangAcitivity.class);
            intent.putExtra("param",param);
            startActivity(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mWebview!=null){
            mWebview.removeJavascriptInterface("reportBridge");
            mWebview.destroy();
        }
    }
}
